#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/11/11 下午1:50
# @Author  : yinxin
# @File    : application
# @Software: PyCharm

import inspect
import time

import sppm

from common import spider_config, list_api_config, retry_time, delay
from log import hlog
from modules import PLATFORM_SPIDER_MAP
from utils import get_start_url, get_platform


def start_list_crawl():
    func_name = inspect.stack()[0][3]
    hlog.enter_func(func_name)

    while True:
        if sppm.signal_monitor():
            hlog.info('任务回调函数返回True，退出......')
            break
        try:
            url, num = get_start_url(list_api_config)
        except:
            hlog.debug("访问接口失败, 准备休眠%ss后重试" % retry_time)
            time.sleep(retry_time)
            continue
        # 初始化爬虫
        if "" == url:
            hlog.debug("未获取到新的url, 准备休眠%ss后重试" % retry_time)
            time.sleep(retry_time)
            continue

        # 锁定工作状态
        sppm.working_lock(True)

        platform = get_platform(url)
        my_spider = PLATFORM_SPIDER_MAP[platform](url,
                                                  num,
                                                  spider_config.spider[platform])
        my_spider.list_handle()

        # 解锁工作状态
        sppm.working_lock(False)

        hlog.debug("已完成起点url: %s, 准备休眠%ss后继续" % (url, delay))
        time.sleep(delay)


if __name__ == '__main__':
    sppm.sppm_start(start_list_crawl)
